package com.yhcui.rabbit.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import com.yhcui.rabbit.qrcode.QrCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.util.RamUsageEstimator;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * 生成pdf文件。
 * 除此之外还可以通过html模板生成
 */
@Slf4j
public class PdfUtils {


    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }


    /**
     * 将qrcode转成pdf
     * @param qrcode
     * @return
     */
    public static byte[] generateQRPdfBytesOfImage(byte[] qrcode) {

        ClassPathResource headerLogoResource = new ClassPathResource("/qrcode/OPay_Logo.png");


        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
             InputStream headerStream = headerLogoResource.getInputStream()) {

            byte[] headerLogoByte = toByteArray(headerStream);
            // RectangleReadOnly(595,842);

            Document doc = new Document(new com.itextpdf.text.Rectangle(PageSize.A4));
            PdfWriter.getInstance(doc,baos);
            doc.open();

            // header_logo
            com.itextpdf.text.Image headerLogoImage = com.itextpdf.text.Image.getInstance(headerLogoByte);
            com.itextpdf.text.Rectangle rectangle = new com.itextpdf.text.Rectangle(0, 0, 200, 83);
            headerLogoImage.scaleAbsolute(rectangle);
            headerLogoImage.setAbsolutePosition(198, 702);
            doc.add(headerLogoImage);

            // header title
            com.itextpdf.text.Font green = new com.itextpdf.text.Font(            com.itextpdf.text.Font
                    .FontFamily.COURIER, 24,             com.itextpdf.text.Font
                    .NORMAL, BaseColor.GREEN);
            green.setColor(34, 47, 62);

            Paragraph headerTitle = new Paragraph("\n\n\n\nSafe, convenient and simple", green);
            headerTitle.setAlignment(Element.ALIGN_CENTER);
            doc.add(headerTitle);
//            // 二维码
            com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(qrcode);
            com.itextpdf.text.Rectangle rectangleImage = new com.itextpdf.text.Rectangle(0, 0, 440, 440);
            image.scaleAbsolute(rectangleImage);
            image.setAbsolutePosition(78, 145);
            doc.add(image);
//
//            // header title
            Paragraph name = new Paragraph("\n\n\n\n\n\n\n\n\n\n\n\n\n\nyuhui.cui", green);
            name.setAlignment(Element.ALIGN_CENTER);
            doc.add(name);


            if(doc != null){
                doc.close();
            }
            byte[] b = baos.toByteArray();
            return b;
        }catch(Exception e) {
            log.error("generateQRPdfBytesOfImage error Exception:", e);
        }
        return null;
    }

    public static void main(String[] args) throws Exception {

        QrCodeUtils.init();
        byte[] data = QrCodeUtils.genQRCodeImageBytes("发送端sadds12312",400, 400);
        data = QrCodeUtils.addLogoImageToQRCodeBytes(data, true);
        byte[] b = PdfUtils.generateQRPdfBytesOfImage(data);
        long size = RamUsageEstimator.sizeOf(b);
        System.out.println("size:" + size);
        String filename = "/Users/yuhuicui/tmp/array2pdf.pdf";
        FileOutputStream fout = new FileOutputStream(filename);
        //将字节写入文件
        fout.write(b);
        fout.close();
    }

}
